<?php

class DeveloperController extends Zend_Controller_Action {

    protected $_log;
    protected $doctrineContainer;

    public function init() {
        $this->_log = Zend_Registry::get('log');
        $this->doctrineContainer = Zend_Registry::get('doctrine');
    }

    public function indexAction() {
        $this->view->headTitle($this->view->translate->_('My Account'), 'PREPEND');
        $manifestXML = new Zend_Config_Xml(APPLICATION_PATH . "/configs/agent-manifest.xml");
       # $manifestXML = new Zend_Config_Xml(APPLICATION_PATH . "/configs/epagesLicense.xml");
        $agent = $manifestXML->toArray();
       # $hubs = $agent['hubs']['hub'];
       # unset($agent['hubs']);
        print_r($agent);
       # print_r($hubs);
    }

    public function dropdbAction() {
        $this->_helper->layout()->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();

        $this->doctrineContainer->getConnection()->close();
        $em = $this->doctrineContainer->getEntityManager();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        $tool->dropDatabase();

        $this->_redirect("developer/index");
    }

    public function createdbAction() {
        $this->_helper->layout()->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();

        $em = $this->doctrineContainer->getEntityManager();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        $tool->dropDatabase();
        $tool->createSchema($em->getMetadataFactory()->getAllMetadata());

        $this->_redirect("developer/index");
    }

    public function agentsAction() {
        $this->_helper->layout()->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();

#        $users = $em->createQuery('select u from Jhub1db\Entity\User u')->execute();
#        
#        foreach ($users as $user) {
#            echo $user->firstname . "<br />";
#        } 

        $em = $this->doctrineContainer->getEntityManager();

        #      $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        #      $tool->dropSchema(Jhub1db\Entity\Jhub1agent);
   
# NOOP
        $agent = new Jhub1db\Entity\Jhub1agent('ghjadf44mbnm324');
        $agent->setDescription("NOOP example agent fake values");
        $agent->setFile("ServiceNOOP.pl");
        $agent->setVisible(1);
        $agent->setInterpreter("/usr/bin/perl");
        $agent->setAname("NOOP");
        $agent->setPath("ServiceNOOP");
        $agent->setConfigpath("ServiceNOOP");
        $agent->setConfigfile("ServiceNOOP.config");
        $agent->setInstances(99);
        $agent->setOrderi(5);
        $agent->setVersion("0.11");
        $agent->setDeveloper("jhub.org");

        $em->persist($agent);

        $adev1 = new Jhub1db\Entity\Jhub1agentdev("NOOP Test Dev");
        $adev1->setIfType("VIR");
        $adev1->setDescription("Test functionality device");
        $adev1->setConfigTemplate("audon.ini");
        $adev1->setFixedDevices("noopdevicestemp.json");
        $adev1->setOnlyFixeddev(0);
        $em->persist($adev1);

        $agent->addDevrecord($adev1);
        
        $em->flush();

        $agent = null;
        $adev1 = null;
     
# ELPROC
        $agent = new Jhub1db\Entity\Jhub1agent('Sdfghf4444mbnm324');
        $agent->setDescription("Ethernet devices / ip watchdogs LD100 and LD200");
        $agent->setFile("asdfsf");
        $agent->setVisible(1);
        $agent->setInterpreter("perl");
        $agent->setAname("ELPROC");
        $agent->setPath("/dfsdf/dfsdfsd");
        $agent->setConfigpath("/sadfsa/config");
        $agent->setConfigfile("sdfsdf.cfg");
        $agent->setInstances(99);
        $agent->setOrderi(10);
        $agent->setVersion("a 0.1");
        $agent->setDeveloper("jhub.org");

        $em->persist($agent);

        $adev1 = new Jhub1db\Entity\Jhub1agentdev("LD 100");
        $adev1->setIfType("ETH");
        $adev1->setDescription("Limited functionality device");
        $adev1->setConfigTemplate("elproc.ini");
        $adev1->setFixedDevices("elprocdevicestemp.json");
        $adev1->setOnlyFixeddev(0);
        $em->persist($adev1);

        $adev2 = new Jhub1db\Entity\Jhub1agentdev("LD 200");
        $adev2->setIfType("ETH");
        $adev2->setDescription("Extended functionality device");
        $adev2->setConfigTemplate("elproc.ini");
        $adev2->setFixedDevices("elprocdevicestemp.json");
        $adev2->setOnlyFixeddev(0);
        $em->persist($adev2);

        $adev3 = new Jhub1db\Entity\Jhub1agentdev("LD 300");
        $adev3->setIfType("VIR");
        $adev3->setDescription("Extended functionality virtual monitor");
        $adev3->setOnlyFixeddev(0);
        $adev3->setConfigTemplate("");
        $em->persist($adev3);

        $agent->addDevrecord($adev1);
        $agent->addDevrecord($adev2);
        $agent->addDevrecord($adev3);

        $em->flush();

        $agent = null;
        $adev1 = null;
        $adev2 = null;
        $adev3 = null;

# AUDON

        $agent = new Jhub1db\Entity\Jhub1agent('Sdfghdsfsdsdfsdfsmbnm324');
        $agent->setDescription("UDIN USB Relay and Opto Input Units");
        $agent->setFile("asdfsf");
        $agent->setVisible(1);
        $agent->setInterpreter("perl");
        $agent->setAname("AUDON");
        $agent->setPath("/dfsdf/dfsdfsd");
        $agent->setConfigpath("/sadfsa/config");
        $agent->setConfigfile("sdfsdf.cfg");
        $agent->setInstances(99);
        $agent->setOrderi(10);
        $agent->setVersion("1.1");
        $agent->setDeveloper("jhub.org");

        $em->persist($agent);

        $adev1 = new Jhub1db\Entity\Jhub1agentdev("UDIN-44");
        $adev1->setIfType("USB");
        $adev1->setDescription("4 x 3A Relays, 4 x opto-isolated Inputs");
        $adev1->setConfigTemplate("");
        $adev1->setFixedDevices("audondevicestemp.json");
        $adev1->setOnlyFixeddev(1);
        $em->persist($adev1);

        $adev2 = new Jhub1db\Entity\Jhub1agentdev("UDIN-8R");
        $adev2->setIfType("USB");
        $adev2->setDescription("8 x 3A Relays");
        $adev2->setConfigTemplate("audon.ini");
        $adev2->setFixedDevices("audondevicestemp.json");
        $adev2->setOnlyFixeddev(1);
        $em->persist($adev2);

        $adev3 = new Jhub1db\Entity\Jhub1agentdev("UDIN-8I");
        $adev3->setIfType("USB");
        $adev3->setDescription("8 x Opto-isolated Inputs");
        $adev3->setConfigTemplate("audon.ini");
        $adev3->setFixedDevices("audondevicestemp.json");
        $adev3->setOnlyFixeddev(1);
        $em->persist($adev3);

        $adev4 = new Jhub1db\Entity\Jhub1agentdev("UDIN-VIR");
        $adev4->setIfType("VIR");
        $adev4->setDescription("Opto-isolated virtual Inputs monitor");
        $adev4->setConfigTemplate("");
        $em->persist($adev4);

        $agent->addDevrecord($adev1);
        $agent->addDevrecord($adev2);
        $agent->addDevrecord($adev3);
        $agent->addDevrecord($adev4);

        $em->flush();

        $agent = null;
        $adev1 = null;
        $adev2 = null;
        $adev3 = null;
        $adev4 = null;

        $this->_redirect("developer/index");
    }

    public function usersAction() {
        $this->_helper->layout()->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();

        $em = $this->doctrineContainer->getEntityManager();

        $user1 = new Jhub1db\Entity\Jhub1user('user');
        $user1->setRole("user");
        $user1->setDesc("Ordynary user");
        $user1->setPassword("user");
        $user1->setName("John");
        $user1->setSurname("Doe");
        $em->persist($user1);
        $em->flush();

        $user2 = new Jhub1db\Entity\Jhub1user('admin');
        $user2->setRole("admin");
        $user2->setDesc("Admin user");
        $user2->setPassword("admin");
        $user2->setName("Marek");
        $user2->setSurname("Kaszewski");
        $em->persist($user2);
        $em->flush();

        $user3 = new Jhub1db\Entity\Jhub1user('developer');
        $user3->setRole("developer");
        $user3->setDesc("Developer user");
        $user3->setPassword("developer");
        $user3->setName("Developer");
        $user3->setSurname("Developer");
        $em->persist($user3);
        $em->flush();

        $this->_redirect("developer/index");
    }

    public function utilitytablesAction() {
        $this->_helper->layout()->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();

        $em = $this->doctrineContainer->getEntityManager();

        $type = new Jhub1db\Entity\Jhub1devicetype('text');
        $type->setAttributes("type,name,id,orderi,pictogram,value");
        $em->persist($type);
        $em->flush();

        $type = new Jhub1db\Entity\Jhub1devicetype('bool');
        $type->setAttributes("type,name,id,orderi,active,pictogram,value,switchtype,truevalue,falsevalue");
        $em->persist($type);
        $em->flush();

        $type = new Jhub1db\Entity\Jhub1devicetype('int');
        $type->setAttributes("type,name,id,orderi,active,pictogram,value,unit,maxlimit,minlimit,maxvalue,minvalue,threshold,static,step");
        $em->persist($type);
        $em->flush();

        $type = new Jhub1db\Entity\Jhub1devicetype('real');
        $type->setAttributes("type,name,id,orderi,active,pictogram,value,unit,maxlimit,minlimit,maxvalue,minvalue,threshold,static,precision");
        $em->persist($type);
        $em->flush();

        $type = new Jhub1db\Entity\Jhub1devicetype('colour');
        $type->setAttributes("type,name,id,orderi,active,pictogram,value");
        $em->persist($type);
        $em->flush();

        $type = new Jhub1db\Entity\Jhub1devicetype('list');
        $type->setAttributes("type,name,id,orderi,active,pictogram,value");
        $em->persist($type);
        $em->flush();

        $this->_redirect("developer/index");
    }

    public function deletechainAction() {
        $this->_helper->layout()->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();
        $em = $this->doctrineContainer->getEntityManager();

#        $dev = $em->getRepository('Jhub1db\Entity\Jhub1agent')->findBy(array('id' => 2));
#        $em->remove($dev[0]);
#        $em->flush();

        $dev = $em->getRepository('Jhub1db\Entity\Jhub1agentdev')->findBy(array('id' => 6));
        $em->remove($dev[0]);
        $em->flush();


        $this->_redirect("developer/index");
    }

}
